package system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.controller.base.BaseController;
import common.model.enums.EmailValidEnum;
import common.model.po.SysLog;
import common.model.po.SysUser;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import common.service.RedissonCacheService;
import common.util.UserChecker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import system.model.request.*;
import system.model.response.MailBoxStatusResponse;
import system.model.response.UpdateMyselfResponse;
import system.service.SysLogService;
import system.service.SysUserService;
import system.service.UserEmailService;

import javax.annotation.Resource;

/**
 * 个人信息管理
 *
 * @author 米泽鹏
 * @since 2022-03-11
 */
@Api(tags = "个人信息管理")
@RestController
@RequestMapping("/system/sys-myself")
public class SysMyselfController extends BaseController {

	@Resource
	private SysUserService userService;
	@Resource
	private SysLogService logService;
	@Resource
	private UserEmailService userEmailService;
	@Resource
	private RedissonCacheService redissonCacheService;

	@ApiOperation(value = "账号删除（延时消息）", notes = "用户登录系统后可以在个人中心页面删除账号所有数据")
	@DeleteMapping("/drawDownMq/{userId}")
	public Result<?> drawDownMq(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
		return userService.sendDrawDownMq(userId);
	}

	@ApiOperation(value = "更新个人信息", notes = "用户登录系统后可以在个人中心页面更新个人信息：昵称、用户名、头像、个人简介")
	@PostMapping("/updateMyself")
	public Result<UpdateMyselfResponse> updateMyself(@ApiParam(value = "UpdateMyselfRequest", required = true) @RequestBody @Validated UpdateMyselfRequest request) {
		return userService.updateMyself(request);
	}

	@ApiOperation(value = "修改登录密码", notes = "用户登录系统后可以在个人中心页面修改登录密码")
	@PostMapping("/updateMyPassword")
	public Result<?> updateMyPassword(@ApiParam(value = "UpdateMyPasswordRequest", required = true) @RequestBody @Validated UpdateMyPasswordRequest request) {
		return userService.updateMyPassword(request);
	}

	@ApiOperation(value = "个人操作日志分页查询", notes = "个人操作日志分页查询", response = Result.class)
	@PostMapping("/pageMyselfSystemLog")
	public Result<Page<SysLog>> pageMyselfSystemLog(@ApiParam(value = "分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchMyselfSystemLogRequest, SysLog> paginationRequest) {
		return successWithData(logService.pageMyselfSystemLog(paginationRequest));
	}

	@ApiOperation(value = "发送邮箱激活邮件", notes = "发送邮箱激活邮件", response = Result.class)
	@PostMapping("/sendActivateEmail")
	public Result<?> sendActivateEmail(@ApiParam(value = "发送邮箱激活邮件请求", required = true) @RequestBody @Validated SendActivateEmailRequest request) {
		return userEmailService.sendMailBoxActivationEmail(request);
	}

	/**
	 * 根据用户id查询邮箱状态
	 *
	 * @param userId 用户id
	 * @return 邮箱状态：
	 * 0-未激活，且不是邮箱换绑状态；
	 * 1-已激活；
	 * 2-未激活，且正在进行邮箱换绑
	 */
	@ApiOperation(value = "根据用户id查询邮箱状态", notes = "根据用户id查询邮箱状态", response = Result.class)
	@GetMapping("/getEmailStatusByUserId/{userId}")
	public Result<MailBoxStatusResponse> getEmailStatusByUserId(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
		SysUser user = userService.getById(userId);
		UserChecker.checkUser(user);
		MailBoxStatusResponse response = new MailBoxStatusResponse();
		response.setEmail(user.getEmail());
		Integer emailValid = user.getEmailValid();
		if (EmailValidEnum.VALID.getValue().equals(emailValid)) {
			response.setStatus(1);
		} else {
			String token = redissonCacheService.getModifyEmailTokenStringByUserId(user.getId());
			if (StrUtil.isNotBlank(token)) {
				response.setStatus(2);
			} else {
				response.setStatus(0);
			}
		}
		return successWithData(response);
	}

	@ApiOperation(value = "修改邮件通知类型", notes = "修改邮件通知类型", response = Result.class)
	@PostMapping("/modifyEmailNotifyTypes")
	public Result<SysUser> modifyEmailNotifyTypes(@ApiParam(value = "修改邮件通知类型请求", required = true) @RequestBody @Validated ModifyEmailNotifyTypesRequest request) {
		return userService.modifyEmailNotifyTypes(request);
	}

}
